<?php

/**
 * login page
 *
 * @author  jiang
 */

namespace Kj\Admin\Controller;

use Lang,URL;
use Kj\Admin\Controller\AdminController;
use Kj\Admin\Libraries\Acl;
use View, Scstore, Input, Redirect, Config, Auth, Session;
use Kj\Admin\Libraries\Js;

class LoginController extends AdminController
{
    /**
     * 用户状态正常标识
     *
     * @var intval
     */
    CONST STATUS = 1;

    /**
     * __construct
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * login page
     * 
     * @return void
     */
    public function index()
    {
        return View::make('admin/login');
    }

    /**
     * process login action
     * 
     * @return void
     */
    public function doo()
    {
        $username = Input::get('username', false);
        $password = Input::get('password', false);
        //密码的字段必须为password，除非你在User模型中重新定义了它。
        $credentials = array(
            'name' => $username, 'password' => $password, 'status' => self::STATUS
        );
        if(Auth::attempt($credentials))
        {
            //设置用户权限的session
            Scstore::setUserPermissionSession(new Acl(), Auth::user());
            //验证成功后跳转到后台主页
            return Js::locate(URL::to(Config::get('admin.login_success_uri')), 'parent');
        }
        else
        {
            return Js::error(Lang::get('bk.account_password_error'));
        }
    }

    /**
     * logout system
     * 
     * @return void
     */
    public function logout()
    {
        Auth::logout();
        Session::flush();
        Session::regenerate();
        return Redirect::intended(
            Config::get('admin.login_uri')
        );
    }

}
